{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block title %}{{ family.name }} - {{ block.super }}{% endblock %}

{% block content %}
<div class="row">
    <!-- 左侧家庭信息 -->
    <div class="col-md-4">
        <div class="card mb-4">
            <div class="card-header bg-primary text-white">
                <h4 class="card-title mb-0">
                    <i class="fas fa-home"></i> {{ family.name }}
                </h4>
            </div>
            <div class="card-body">
                <p class="card-text">
                    <i class="fas fa-crown"></i> 创建者：{{ family.creator.username }}
                </p>
                <p class="card-text">
                    <i class="fas fa-users"></i> 成员数：{{ family.members.count }}
                </p>
                <p class="card-text">
                    <i class="fas fa-calendar"></i> 创建时间：{{ family.created_at|date:"Y-m-d" }}
                </p>
            </div>
        </div>

        {% if is_admin %}
        <!-- 添加成员表单（仅管理员可见） -->
        <div class="card mb-4">
            <div class="card-header bg-primary text-white">
                <h5 class="card-title mb-0">
                    <i class="fas fa-user-plus"></i> 添加成员
                </h5>
            </div>
            <div class="card-body">
                <form method="post" novalidate>
                    {% csrf_token %}
                    <div class="mb-3">
                        <label for="username" class="form-label">用户名</label>
                        <input type="text" class="form-control" id="username" name="username" required>
                        <div class="form-text">输入要添加的用户名</div>
                    </div>
                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-plus"></i> 添加成员
                        </button>
                    </div>
                </form>
            </div>
        </div>
        {% endif %}
    </div>

    <!-- 右侧成员列表 -->
    <div class="col-md-8">
        <div class="card">
            <div class="card-header bg-primary text-white">
                <h4 class="card-title mb-0">
                    <i class="fas fa-users"></i> 家庭成员
                </h4>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>用户名</th>
                                <th>昵称</th>
                                <th>角色</th>
                                <th>加入时间</th>
                                {% if is_admin %}
                                <th>操作</th>
                                {% endif %}
                            </tr>
                        </thead>
                        <tbody>
                            {% for member in family.familymember_set.all %}
                            <tr>
                                <td>{{ member.user.username }}</td>
                                <td>{{ member.user.profile.nickname|default:"-" }}</td>
                                <td>
                                    {% if member.role == 'admin' %}
                                        <span class="badge bg-primary">管理员</span>
                                    {% else %}
                                        <span class="badge bg-secondary">成员</span>
                                    {% endif %}
                                </td>
                                <td>{{ member.joined_at|date:"Y-m-d" }}</td>
                                {% if is_admin and member.user != family.creator %}
                                <td>
                                    <button class="btn btn-danger btn-sm">
                                        <i class="fas fa-user-minus"></i> 移除
                                    </button>
                                </td>
                                {% endif %}
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %} 